package org.nowcoder.leetcode.twoPointers;

import java.util.ArrayList;
import java.util.List;

/**
 * Title  : 86. Partition List
 * Source : https://leetcode.cn/problems/partition-list/description/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-08-07
 */

public class LeetCode_86 {
    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode partition(ListNode head, int x) {
        //边界条件
        if (head == null || head.next == null) {
            return head;
        }
        List<Integer> minX = new ArrayList<>();
        List<Integer> maxX = new ArrayList<>();
        ListNode p = head;
        while (p != null) {
            if (p.val < x) {
                minX.add(p.val);
            } else {
                maxX.add(p.val);
            }
            p = p.next;
        }
        p = head;
        for (Integer i : minX) {
            p.val = i;
            p = p.next;
        }
        for (Integer i : maxX) {
            p.val = i;
            p = p.next;
        }
        return head;
    }
}
